from django.shortcuts import render
from .models import Post
from django.http import HttpResponse
from django.http import JsonResponse
# Create your views here.


def get_post_list(request):
    return render(request, 'blog/post_list.html', {'car':Post.get_sum()})


def show_item(request):
    database = Post.objects.all()
    if request.method == "POST":
        item = Post.objects.filter(id=(request.POST['id']))
        if item:
            item[0].counts+=1
            item[0].save()
        return HttpResponse(Post.sum())
    return render(request, 'blog/shop.html', {'posts':database, 'car':Post.get_sum()})


def modify_cat_info(request):
    item_info = Post.objects.exclude( counts = 0 )
    Post.total()
    if request.method == 'POST':
        item = Post.objects.filter(id=(request.POST['id']))
        hit=(request.POST['hit'])
        if (item and hit == '1') :
            item[0].counts+=1
            item[0].save()
            if (item[0].counts >= 3 and item[0].offer == 1):
                item[0].send = int(item[0].counts/3)
                total = float(item[0].price) * (int(item[0].counts - item[0].send))
                original = float(item[0].price) * (int(item[0].counts));
                item[0].subtotal = str(total) + '元' + '(' + '原价' + '\n' + str(original) + '元' + ')'
                item[0].save()
            elif(item[0].counts > 0):
                item[0].send = 0;
                original = float(item[0].price) * (int(item[0].counts));
                item[0].subtotal = str(original) + '元';
                item[0].save()
        elif (item and hit == '-1'and item[0].counts != 0):
            item[0].counts-=1
            item[0].save()
            if (item[0].counts >= 3 and item[0].offer == 1):
                item[0].send = int(item[0].counts / 3)
                total = float(item[0].price) * (int(item[0].counts - item[0].send))
                original = float(item[0].price) * (int(item[0].counts));
                item[0].subtotal = str(total) + '元' + '(' + '原价' + '\n' + str(original) + '元' + ')'
                item[0].save()
            elif(item[0].counts > 0):
                item[0].send = 0;
                original = float(item[0].price) * (int(item[0].counts));
                item[0].subtotal = str(original) + '元';
                item[0].save()
        return JsonResponse({'counts': item[0].counts, 'sum': Post.sum(),'sub':Post.get_total(),'check':item[0].subtotal})
    return render(request, 'blog/car.html', {'car':Post.get_sum(),'postss':item_info,'total':Post.get_total(),'subtal':Post.total()})


def base(request):
    return render(request, 'blog/base.html',{'car':Post.get_sum()})


def show_list(request):
    database =Post.objects.all()
    item_info = Post.objects.exclude(counts=0);
    free_goods = [];
    for item in item_info:
        single = {};
        if (item.counts >= 3 and item.offer==1):
            item.send = (item.counts // 3)
            single['name'] = item.name;
            single['sort'] = item.sort;
            single['send'] = item.send;
            free_goods.append(single);
            item.save();
        else:
            item.send = 0;
            item.save();
    import time;
    time=time.strftime("%Y年%m月%d日 %H:%M:%S", time.localtime())
    if request.method == 'POST':
        determine = (request.POST['determine'])
        if determine == 'over':
          for item in database:
              item.counts = 0;
              item.send = 0;
              item.subtotal = '';
              item.save()
        return JsonResponse({'number':Post.get_sum()})
    return render(request, 'blog/list.html',{'car':Post.get_sum(),'posts':item_info,'time':time,'save':free_goods,'total':Post.get_total(),'saving':Post.saving})
